import { listDept, treeselect } from '@/api/system/dept'
import { getAllCgList } from '@/api/project/mstr'
import { getAll } from '@/api/repertory/Storage'
const options = {
  state: {
    productType: [],
    productTypeTree: [],
    dept: [],
    deptTree: [],
    warehouse: [],
    bomCgsxOptions: [],
    allCwOptions: [],
  },

  mutations: {
    SET_DEPT: (state, dept) => {
      state.dept = dept.map((o) => {
        return { ...o, dictLabel: o.deptName, dictValue: o.deptId }
      })
    },
    SET_DEPTTREE: (state, dept) => {
      state.deptTree = dept
    },
    SET_WAREHOUSE: (state, data) => {
      state.warehouse = data
    },
    SET_BomCgsx: (state, data) => {
      state.bomCgsxOptions = data
    },
    SET_CwOpt: (state, data) => {
      state.allCwOptions = data
    },
  },

  actions: {
    // 获取用户部门信息
    GetDept({ commit, state }) {
      return new Promise((resolve, reject) => {
        listDept()
          .then((res) => {
            commit('SET_DEPT', res.data)
            resolve(res)
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    /** 查询部门下拉树结构 */
    GetDeptTree({ commit, state }) {
      return new Promise((resolve, reject) => {
        treeselect()
          .then((res) => {
            commit('SET_DEPTTREE', res.data)
            resolve(res)
          })
          .catch((error) => {
            reject(error)
          })
      })
    },

    GetBomCgsx({ commit, state }) {
      return new Promise((resolve, reject) => {
        getAllCgList()
          .then((response) => {
            commit(
              'SET_BomCgsx',
              response.data.map((o) => {
                return { ...o, dictLabel: o.cgName, dictValue: o.cgId + '', listClass: 'default' }
              })
            )
            resolve(res)
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    GetCwOpt({ commit, state }) {
      return new Promise((resolve, reject) => {
        getAll()
          .then((response) => {
            commit(
              'SET_CwOpt',
              response.data.map((o) => {
                return { ...o, dictLabel: o.storageName, dictValue: o.storageId + '', listClass: 'default' }
              })
            )
            resolve(res)
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
  },
}

export default options
